package com.ruoyi.activiti.modeler.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ruoyi.activiti.modeler.domain.CompletionTaskEntity;
import com.ruoyi.activiti.modeler.domain.StartingProcessEntity;
import com.ruoyi.activiti.modeler.domain.TaskModelEntity;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 流程控制
 */
@RestController
public class ActivitiRest {
    // 保存流程

    // 新增流程
    @RequestMapping("activiti/create")
    public void createModel(HttpServletRequest request, HttpServletResponse response){
        try{
            // 新建model
            ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
            RepositoryService repositoryService = processEngine.getRepositoryService();
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode editorNode = objectMapper.createObjectNode();
            editorNode.put("id", "canvas");
            editorNode.put("resourceId", "canvas");
            ObjectNode stencilSetNode = objectMapper.createObjectNode();
            stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
            editorNode.put("stencilset", stencilSetNode);
            Model modelData = repositoryService.newModel();
            //保存模型
            repositoryService.saveModel(modelData);
            repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
            response.sendRedirect(request.getContextPath() + "/modeler/modeler.html?modelId=" + modelData.getId());
        }catch (Exception e){
            throw new RuntimeException("流程创建初始化异常");
        }
    }

    /**
     * 获得流程信息
     */
    @RequestMapping("activiti/get")
    public void getModel(HttpServletRequest request, HttpServletResponse response, String modelId){
        try {
            response.sendRedirect(request.getContextPath() + "/modeler/modeler.html?modelId=" + modelId);
        } catch (IOException e) {
            throw new RuntimeException("流程查看异常");
        }
    }

    // 编辑/保存流程
    @RequestMapping("activiti/save")
    public void saveModel(String xml){
        // 流程key和名字与model的key和名字一样
        // 保存model
        // 保存流程
    }

    /**
     * 部署bpmn流程
     *
     * @param bpmnModel
     * @return 部署标识
     */
    @RequestMapping("activiti/deployBpmnModel")
    public String deployBpmnModel(String bpmnModel){
        return "";
    }

    /**
     * 获取流程模型
     *
     * @param processDefinitionId 流程模型标识
     * @return 流程模型
     */
    @RequestMapping("activiti/getBpmnModel")
    public String getBpmnModel(String processDefinitionId){
        return "";
    }

    /**
     * 启动流程实例
     *
     * @param entity 流程定义标识
     * @return 流程实例信息
     */
    @RequestMapping("activiti/startWorkflow")
    public String startWorkflow(@RequestBody StartingProcessEntity entity){
        return "";
    }

    /**
     * 取消流程实体
     * @param topic 流程定义topic或defKey
     * @param bizKey 业务标识
     */
    @RequestMapping("activiti/cancelWorkflow")
    public String cancelWorkflow(String topic, String bizKey){
        return "";
    }

    /**
     * 完成任务
     *
     * @param entity 完成任务参数
     */
    public void completeTask(@RequestBody CompletionTaskEntity entity){

    }

    /**
     * 显示流程图
     */
    public View showProcessImage(String processDefinitionId, String processInstanceId){
        return new View() {
            @Override
            public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {

            }
        };
    }

    /**
     * 加签：新增任务审批用户
     */
    public void addTaskApprover(String taskId, @RequestBody List<String> userList){

    }

    /**
     * 改签：修改任务审批用户
     */
    public void updateTaskApprover(String taskId, @RequestBody List<String> userList){

    }

    /**
     * 退回：回退到上个节点
     */
    public void rollBackByApprover(@RequestBody TaskModelEntity model){

    }

    /**
     * 设置代理
     * creatorId：创建人id；agentId：代理人id
     */
    public void setAgent(String creatorId, String agentId){

    }

}
